package web;

import ejb.CategoryBeanLocal;
import entities.Category;
import java.io.IOException;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import org.primefaces.event.RowEditEvent;
import org.primefaces.event.SelectEvent;
import web.models.CategoryModel;

/**
 *
 * @author Jesus
 */
@ManagedBean
public class AdmCategoriesBean {

    @EJB
    private CategoryBeanLocal categoryBean;
    private CategoryModel categories;
    private Category currentCategory;
    private String categoryName;

    public AdmCategoriesBean() {
    }

    @PostConstruct
    public void init() {
        categories = new CategoryModel(categoryBean.findAll());
    }

    public CategoryModel getCategories() {
        return categories;
    }

    public void setCategories(CategoryModel categories) {
        this.categories = categories;
    }

    public Category getCurrentCategory() {
        return currentCategory;
    }

    public void setCurrentCategory(Category currentCategory) {
        this.currentCategory = currentCategory;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    public void onRowSelect(SelectEvent event) {
        currentCategory = (Category) event.getObject();
        categoryName = currentCategory.getName();
    }

    public void insertCategory() {
        System.out.println("inscat");
        currentCategory = new Category(categoryName);
        if (categoryName != null && !categoryName.trim().equals("")) {
            Category c = categoryBean.findByName(categoryName.trim());
            if (c == null) {
                currentCategory = categoryBean.insert(currentCategory);
                if (currentCategory != null) {
                    categories.add(currentCategory);
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "La categoría " + currentCategory.getName() + " ha sido insertada.", ""));
                    cleanForm();
                    return;
                }
            }
        }
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Ha ocurrido un error al guardar la categoría.", ""));
    }

    public void cleanForm() {
        currentCategory = new Category();
        categoryName = "";
    }

    public void onRowEdit(RowEditEvent event) {
        Category c = (Category) event.getObject();
        c = categoryBean.update(c);
        if (c != null) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "La categoría " + c.getName() + " ha sido actualizada", ""));
        } else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Ha ocurrido un error al actualizar la categoría.", ""));
        }
    }

    public SelectItem[] getVisibilities() {
        return new SelectItem[]{
            new SelectItem(Boolean.TRUE, "Sí"),
            new SelectItem(Boolean.FALSE, "No")};
    }

    public void goToCategory(Long id) {
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("/Tiendalia-war/tiendalia/main.xhtml?category=" + id.toString());
        } catch (IOException ex) {
        }
    }
}
